﻿using System;
using System.Collections.Generic;
using CRMFramework;
using CRMBiz;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRMWeb;
using System.IO;
using System.Web.Services;
using System.Data;
using System.Xml;
using System.Xml.Serialization;
using System.Text;


namespace CRMWeb.Pages.Group
{
    public partial class GroupDetail : CRMWeb.Utilities.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string strFilePath = string.Empty;
                string strQuery = hdnDesc.Value;
                string strdays = string.Empty;
                string grouptype = hdnType.Value;
                string strcols = " ContactID,FirstName,SurName,Email,Mobile,client_CreateDate as CreateDate ";
                strdays = hdnDays.Value;
                DataTable dtDataTablesList;

                if (strdays == "30")
                    strdays = " DATEDIFF(CURDATE(),DATE(EditDate)) <= 30 ";
                else if (strdays == "90")
                    strdays = "  DATEDIFF(CURDATE(),DATE(EditDate)) <= 90 ";
                else
                    strdays = string.Empty;

                if (grouptype == "Direct Client")
                {
                    if (!string.IsNullOrEmpty(strdays))
                        strQuery += " AND " + strdays;

                    strQuery += " Group By ContactID";
                    dtDataTablesList = DBhelper.ExecuteDataset(strQuery).Tables[0];
                }
                else
                {
                    strQuery = strQuery.Replace("*", strcols);
                    if (!string.IsNullOrEmpty(strdays))
                        strQuery += " AND " + strdays;

                    strQuery += " Group By ContactID";
                    dtDataTablesList = DBhelper.ExecuteDataset(strQuery).Tables[0];
                }
                strFilePath = System.Web.HttpContext.Current.Server.MapPath("/ExportGroupData/groupdata.csv");

                StreamWriter sw = new StreamWriter(strFilePath, false);
                int iColCount = dtDataTablesList.Columns.Count;

                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dtDataTablesList.Columns[i]);
                    if (i < iColCount - 1)
                        sw.Write(",");
                }
                sw.Write(sw.NewLine);

                foreach (DataRow dr in dtDataTablesList.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                            sw.Write(dr[i].ToString());
                        if (i < iColCount - 1)
                            sw.Write(",");
                    }
                    sw.Write(sw.NewLine);
                }
                sw.Close();

                FileInfo fi = new FileInfo(strFilePath);
                if (fi.Exists)
                {
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fi.Name);
                    HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
                    HttpContext.Current.Response.ContentType = "application/.csv";
                    HttpContext.Current.Response.TransmitFile(strFilePath);
                    HttpContext.Current.Response.Flush();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                System.IO.File.Delete(Server.MapPath("/ExportGroupData/groupdata.csv"));
                HttpContext.Current.Response.End();
            }
        }
    }

}